//
//  HYDeviceTableViewCell.m
//  TJHYHardwareProduct
//
//  Created by Kendama on 2018/1/23.
//  Copyright © 2018年 Kendama. All rights reserved.
//

#import "HYDeviceTableViewCell.h"
#import "HYDeviceInfoModel.h"

@implementation HYDeviceTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)updateDeviceInfo:(HYDeviceInfoModel *)model
{
    _deviceIDLabel.text = model.dno;
    _companyLabel.text = model.company_name;
    _adressLabel.text = [NSString stringWithFormat:@"%@栋%@层%@房",model.dong,model.ceng,model.fang];
        // 通讯异常 ，正常 ， 其余的都是警告
    if ([model.status isEqualToString:@"正常"])
    {
        [_stateButton setTitle:@"正常" forState:UIControlStateNormal];
        [_stateButton setBackgroundImage:imageWithName(@"normal") forState:UIControlStateNormal];

    }
    else if ([model.status isEqualToString:@"通讯异常"])
    {
        [_stateButton setTitle:@"失联" forState:UIControlStateNormal];
        [_stateButton setBackgroundImage:imageWithName(@"Out of contact") forState:UIControlStateNormal];
    }
    else
    {
        [_stateButton setTitle:@"报警" forState:UIControlStateNormal];
        [_stateButton setBackgroundImage:imageWithName(@"Call the police") forState:UIControlStateNormal];
    }
    
    if ([model.control_status isEqualToString:@"0"])
    {
        _switchButton.on = NO;
    }
    else
    {
        _switchButton.on = YES;
    }

}

#pragma mark --- 点击事件
- (void)didClickEditButton
{
    if (self.editBlock)
    {
        self.editBlock();
    }
}

- (void)didClickCheckButton
{
    if (self.checkBlock)
    {
        self.checkBlock();
    }
}

- (void)didClickSwitchButton
{
    if (self.switchBlock)
    {
        if (self.switchButton.isOn)
        {
            self.switchBlock(1);
        }
        else
        {
            self.switchBlock(0);
        }
    }
}

#pragma mark --- 懒加载
- (UILabel *)deviceIDLabel
{
    if(!_deviceIDLabel)
    {
        UIView *superView = self.contentView;
        _deviceIDLabel = [[UILabel alloc]init];
        [superView addSubview:_deviceIDLabel];
        [_deviceIDLabel setFont:[UIFont boldSystemFontOfSize:17]];
        [_deviceIDLabel setTextColor:rgba(1,71,141,1)];
    }
    return _deviceIDLabel;
}
- (UILabel *)companyLabel
{
    if(!_companyLabel)
    {
        UIView *superView = self.contentView;
        _companyLabel = [[UILabel alloc]init];
        [superView addSubview:_companyLabel];
        [_companyLabel setFont:[UIFont boldSystemFontOfSize:16]];
        [_companyLabel setTextColor:rgba(51,51,51,1)];
    }
    return _companyLabel;
}
- (UILabel *)adressLabel
{
    if(!_adressLabel)
    {
        UIView *superView = self.contentView;
        _adressLabel = [[UILabel alloc]init];
        [superView addSubview:_adressLabel];
        [_adressLabel setFont:fontWithSize(14)];
        [_adressLabel setTextColor:rgba(155,155,155,1)];
        _adressLabel.numberOfLines = 0;
    }
    return _adressLabel;
}
- (UIButton *)stateButton
{
    if(!_stateButton)
    {
        UIView *superView = self.contentView;
        _stateButton = [[UIButton alloc]init];
        [superView addSubview:_stateButton];
        [_stateButton setTitle:@"正常" forState:UIControlStateNormal];
        [_stateButton setTitle:@"失联" forState:UIControlStateSelected];
        [_stateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_stateButton.titleLabel setFont:fontWithSize(15)];
        [_stateButton setBackgroundImage:imageWithName(@"normal") forState:UIControlStateNormal];
        [_stateButton setBackgroundImage:imageWithName(@"Out of contact") forState:UIControlStateSelected];

    }
    return _stateButton;
}
- (UIImageView *)lineImageView
{
    if(!_lineImageView)
    {
        UIView *superView = self.contentView;
        _lineImageView = [[UIImageView alloc]init];
        [superView addSubview:_lineImageView];
        _lineImageView.image = [UIImage imageNamed:@"line_image"];
//        _lineImageView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    }
    return _lineImageView;
}
- (UIButton *)checkButton
{
    if(!_checkButton)
    {
        UIView *superView = self.contentView;
        _checkButton = [[UIButton alloc]init];
        [superView addSubview:_checkButton];
        [_checkButton setTitle:@"数据查询" forState:UIControlStateNormal];
        [_checkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_checkButton.titleLabel setFont:fontWithSize(15)];
        [_checkButton setBackgroundImage:imageWithName(@"check_button") forState:UIControlStateNormal];
        [_checkButton addTarget:self action:@selector(didClickCheckButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _checkButton;
}
- (UIButton *)editButton
{
    if(!_editButton)
    {
        UIView *superView = self.contentView;
        _editButton = [[UIButton alloc]init];
        [superView addSubview:_editButton];
        [_editButton setTitle:@"编辑" forState:UIControlStateNormal];
        [_editButton setTitleColor:rgba(63,158,255,1) forState:UIControlStateNormal];
        [_editButton.titleLabel setFont:fontWithSize(15)];
        [_editButton setBackgroundImage:imageWithName(@"edit_button") forState:UIControlStateNormal];
        [_editButton addTarget:self action:@selector(didClickEditButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editButton;
}

- (UISwitch *)switchButton
{
    if(!_switchButton)
    {
        UIView *superView = self.contentView;
        _switchButton = [[UISwitch alloc]init];
        [superView addSubview:_switchButton];
        [_switchButton addTarget:self action:@selector(didClickSwitchButton) forControlEvents:UIControlEventValueChanged];
    }
    return _switchButton;
}


#pragma mark --- 布局
- (void)masLayoutSubview
{
    __weak UIView *superView = self.contentView;
    CGFloat edge = px1920Height(30);
    
    [self.stateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(superView);
    }];
    
    [self.deviceIDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(edge);
        make.left.mas_equalTo(superView.mas_left).with.offset(edge);
    }];
    
    [self.companyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_deviceIDLabel.mas_bottom).with.offset(edge);
        make.left.mas_equalTo(_deviceIDLabel.mas_left);
    }];
    
    [self.adressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_companyLabel.mas_bottom).with.offset(edge);
        make.left.mas_equalTo(_deviceIDLabel.mas_left);
        make.right.mas_equalTo(superView.mas_right).with.offset(-70);
        make.height.mas_equalTo(px1920Height(140));
    }];
    
    [self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(superView.mas_right).with.offset(-edge);
        make.centerY.mas_equalTo(_adressLabel.mas_centerY);
    }];
    
    [self.lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_adressLabel.mas_bottom).with.offset(edge);
        make.left.mas_equalTo(superView.mas_left).with.offset(edge);
        make.right.mas_equalTo(superView.mas_right).with.offset(-edge);
        make.height.mas_equalTo(1);
    }];
    
    [self.checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_lineImageView.mas_bottom).with.offset(edge);
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(160));
    }];
    
    [self.editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_checkButton.mas_centerY);
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(160));
    }];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
